package com.yzt.act.service.impl;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.yzt.act.entity.Evection;
import com.yzt.act.kit.SearchKit;
import com.yzt.act.mapper.EvectionMapper;
import com.yzt.act.service.IEvectionService;
import com.yzt.base.entity.BaseUser;
import com.yzt.base.service.impl.BaseServiceImpl;
import com.yzt.base.utils.SecurityUtils;

/**
 * <p>
 * 事件概述表 服务实现类
 * </p>
 *
 * @author Y
 * @since 2021-04-08
 */
@Service
public class EvectionServiceImpl extends BaseServiceImpl<EvectionMapper, Evection> implements IEvectionService {

	@Autowired
	private TaskService taskService;
	@Autowired
	private RuntimeService runtimeService;

	@Override
	public PageInfo<Evection> getEvectionList(SearchKit kit) {
		PageInfo<Evection> info = PageHelper.startPage(kit.getPage(), kit.getLimit()).doSelectPageInfo(
				() -> this.list(Wrappers.<Evection>lambdaQuery().eq(Evection::getUserId, kit.getUserId())));
		return info;
	}

	@Override
	public PageInfo<Evection> getEvectionProcessList(SearchKit kit) {
		BaseUser user = SecurityUtils.getUser();
		List<Task> list = new ArrayList<Task>();
		List<Task> list2 = taskService.createTaskQuery().taskAssignee(user.getUsername()).list();
		List<Task> list3 = taskService.createTaskQuery().taskCandidateUser(user.getUsername()).list();
		list.addAll(list2);
		list.addAll(list3);
		List<String> ids = list.stream().map(Task::getBusinessKey).collect(Collectors.toList());
		if (ids.size() == 0) {
			return null;
		}
		PageInfo<Evection> info = PageHelper.startPage(kit.getPage(), kit.getLimit())
				.doSelectPageInfo(() -> this.list(Wrappers.<Evection>lambdaQuery().in(Evection::getId, ids)));
		return info;
	}

	@Override
	public boolean completeEvection(String eid) {
		boolean complete = false;
		BaseUser user = SecurityUtils.getUser();
		Task task = taskService.createTaskQuery().processInstanceBusinessKey(eid).taskCandidateUser(user.getUsername())
				.singleResult();
		if (task != null) {// 查询到候选人
			// 拾取任务
			taskService.claim(task.getId(), user.getUsername());
			// 重新查询任务
			task = taskService.createTaskQuery().processInstanceBusinessKey(eid).taskAssignee(user.getUsername())
					.singleResult();
			if (task != null) {
				// 完成任务
				taskService.complete(task.getId());
				complete = true;
			}
		} else {
			task = taskService.createTaskQuery().processInstanceBusinessKey(eid).taskAssignee(user.getUsername())
					.singleResult();
			if (task != null) {
				taskService.complete(task.getId());
				complete = true;
			}
		}

		if (complete) {
			task = taskService.createTaskQuery().processInstanceBusinessKey(eid).singleResult();
			if (task == null) {
				Evection e = new Evection();
				e.setId(Long.valueOf(eid));
				e.setStatus("1");
				this.updateById(e);
			}
			return true;
		}

		return false;
	}

	@Override
	public boolean deleteEvection(String eid) {
		Task task = taskService.createTaskQuery().processInstanceBusinessKey(eid).singleResult();
		runtimeService.deleteProcessInstance(task.getProcessInstanceId(), "取消");
		return true;
	}

}
